package com.ctz.commonframe.ui.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by 陈堂正 on 2016/12/5.
 */
public abstract class BaseRecycleAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected Context context;
    protected List<T> dataList;
    protected LayoutInflater inflater;
    protected int layoutId;

    public BaseRecycleAdapter(Context context, List<T> dataList, int layoutId) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.dataList = dataList;
        this.layoutId = layoutId;
    }

    public abstract void convert(BaseRecycleViewHolder holder, int position, T data);

    @Override
    public BaseRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BaseRecycleViewHolder(inflater.inflate(layoutId, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        BaseRecycleViewHolder viewHolder = (BaseRecycleViewHolder) holder;
        T data = dataList.get(position);
        convert(viewHolder, position, data);
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }
}
